Ext.define('Intranet.Desktop.controller.Toolbox', {
    extend: 'Ext.app.Controller',

    views: [
	'Intranet.Desktop.view.Toolbox',
        'Intranet.Desktop.view.InstalledModules',
        'Intranet.Desktop.view.ChangePass'
    ],

    init : function() {
        this.control({

            'panel button[id=logoutBtn]' : {
                click : this.logoutFn
            },
            
            'panel button[id=changePasswordBtn]' : {
                click : this.changePassFn
            },
            
            'panel button[id=installedModBtn]' : {
                click : this.showInstalledModFn
            },
            
            'panel button[id=loginBtn]' : {
                click : this.loginFn
            },
            
            'panel button[id=storeBtn]' : {
                click : this.showStoreFn
            }
        });
    },
            
    logoutFn : function () 
    {
        Ext.Ajax.request({
            url: '/src/public/user/auth/logout',
            success: function(response) {
                var data = Ext.decode(response.responseText);
                if(data.success === 'true')
                    {
                        var teamgroupsPanelCmp = Ext.getCmp('teamgroups_panel');
                        
                        teamgroupsPanelCmp.removeAll();
                        
                        var personalPanelCmp = Ext.getCmp('personal_panel');
                        
                        personalPanelCmp.removeAll();
                        
                        
                        var toolboxCmp = Ext.getCmp('toolbox');
                        
                        toolboxCmp.removeAll();
                        
                        toolboxCmp.add({
                            xtype: 'button',
                            text: 'Login',
                            id: 'loginBtn',
                            width: 80,
                            margin: '0 10 0 0'
                            
                        });
                        
                        var mainscreenCmp = Ext.getCmp('mainscreen');
                        
                        mainscreenCmp.doLayout();
                        
                        Ext.create('Intranet.Desktop.model.UserSetting').getProxy().clear();
                        
                        Ext.Msg.alert('Logout', data.msg);
                    }
            }
        });
    },
    
    loginFn : function(){
        window.location.href = "http://zf2-dev.localhost";
    },
            
    showInstalledModFn : function()
    {
        var modwindow = Ext.create('Ext.window.Window', {
            title: 'Installed Modules',
            width: 600,
            height: 400,
            layout: 'fit',
            
            items: {  
                xtype: 'installedModules'
            }
        });
        
        modwindow.show();
        
        modwindow.alignTo('installedModBtn', 'tr-b', [0, 5]);
        
        
    },
            
    changePassFn : function()
    {
        var changePassForm = Ext.create('Intranet.Desktop.view.ChangePass');
        
        var passwindow = Ext.create('Ext.window.Window', {
            title: 'Change password',
            
            layout: 'fit',
            id: 'change-pass-window',
            height: 140,
            width: 300,
            
            items: [changePassForm]
            
        });
        
        passwindow.show();

    },
            
    showStoreFn : function() {
        var modwindow = Ext.create('Ext.window.Window', {
            title: 'Modules Store',
            
            layout: 'fit',

            items: {  
                xtype: 'modulesStore'
            }
        });
        
        modwindow.show();
        
        modwindow.alignTo('storeBtn', 'tr-bl', [200, 10]);
    }
    
});


     
    